﻿using BCCommon;
using BCCommon.Enums;
using BCData.TMS.OrderExtentionGoodsChecklist;
using BCDto.TMS.OrderExtentionGoodsChecklist;
using BCEntity.TMS.OrderExtentionGoodsChecklist;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BCService.TMS.OrderExtentionGoodsChecklist
{
	public class OrderExtentionGoodsChecklistService : IOrderExtentionGoodsChecklistService
	{
		private readonly IDatabaseContext databaseContext;
		private readonly IRedisService redisService;
		private readonly IOrderExtentionGoodsChecklistData orderExtentionGoodsChecklistData;
		private readonly string key = string.Format("{0}", EntityNameConst.OrderExtentionGoodsChecklistEntity);

		public OrderExtentionGoodsChecklistService(IDatabaseContext databaseContext,
			IRedisService redisService,
			IOrderExtentionGoodsChecklistData orderExtentionGoodsChecklistData)
		{
			this.databaseContext = databaseContext;
			this.redisService = redisService;
			this.orderExtentionGoodsChecklistData = orderExtentionGoodsChecklistData;
		}

		#region 检查订单扩展货物清单信息是否存在
		public bool Exists(long orderExtentionGoodsChecklistId)
		{
			return this.orderExtentionGoodsChecklistData.Exists(orderExtentionGoodsChecklistId).GetAwaiter().GetResult();
		}
		#endregion

		#region 添加订单扩展货物清单信息
		public OrderExtentionGoodsChecklistDto Add(OrderExtentionGoodsChecklistRequestDto requestDto)
		{
			var entity = requestDto.As<OrderExtentionGoodsChecklistEntity>();

			return this.orderExtentionGoodsChecklistData.Add(entity).GetAwaiter().GetResult().As<OrderExtentionGoodsChecklistDto>();
		}
		#endregion

		#region 修改订单扩展货物清单信息
		public OrderExtentionGoodsChecklistDto Update(OrderExtentionGoodsChecklistPutDto putDto)
		{
			if (!this.Exists(putDto.OrderExtentionGoodsChecklistId))
			{
				throw new ArgumentException("此记录不存在!");
			}
			var entity = putDto.As<OrderExtentionGoodsChecklistEntity>();
			var result = this.orderExtentionGoodsChecklistData.Update(entity).GetAwaiter().GetResult().As<OrderExtentionGoodsChecklistDto>();
			string cacheKey = this.redisService.GetKey(key, entity.OrderExtentionGoodsChecklistId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 删除订单扩展货物清单信息
		public bool Delete(long orderExtentionGoodsChecklistId)
		{
			if (!this.Exists(orderExtentionGoodsChecklistId))
			{
				throw new ArgumentException("此记录不存在!");
			}

			var result = this.orderExtentionGoodsChecklistData.Delete(orderExtentionGoodsChecklistId).GetAwaiter().GetResult();
			string cacheKey = this.redisService.GetKey(key, orderExtentionGoodsChecklistId);
			this.redisService.DeleteAsync(cacheKey);
			return result;
		}
		#endregion

		#region 获取订单扩展货物清单信息
		public OrderExtentionGoodsChecklistDto Get(long orderExtentionGoodsChecklistId)
		{
			string cacheKey = this.redisService.GetKey(key, orderExtentionGoodsChecklistId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderExtentionGoodsChecklistData.GetEntity(orderExtentionGoodsChecklistId).GetAwaiter().GetResult();
				return data;
			}, TimeSpan.FromDays(1));
			return result.As<OrderExtentionGoodsChecklistDto>();
		}
		#endregion

		#region 获取订单扩展货物清单信息列表
		public IEnumerable<OrderExtentionGoodsChecklistDto> GetList(string orderNo)
		{
			string cacheKey = this.redisService.GetKey(key, orderNo);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderExtentionGoodsChecklistData.GetList(orderNo).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderExtentionGoodsChecklistDto>>();
		}
		public IEnumerable<OrderExtentionGoodsChecklistDto> GetList(long orderId)
		{
			string cacheKey = this.redisService.GetKey(key, orderId);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				return this.orderExtentionGoodsChecklistData.GetList(orderId).GetAwaiter().GetResult();
			}, TimeSpan.FromDays(1));
			return result.As<IEnumerable<OrderExtentionGoodsChecklistDto>>();
		}
		#endregion

		#region 获取订单扩展货物清单信息分页
		public PageableList<OrderExtentionGoodsChecklistDto> Query(DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			string cacheKey = this.redisService.GetKey(key, start, end, sortName, ascending, pageIndex, pageSize);
			var result = this.redisService.TryGet(cacheKey, () =>
			{
				var data = this.orderExtentionGoodsChecklistData.Query(start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
				return new PageableList<OrderExtentionGoodsChecklistDto>
				{
					Count = data.Item2,
					Items = data.Item1.As<IEnumerable<OrderExtentionGoodsChecklistDto>>(),
					PageIndex = pageIndex,
					PageSize = pageSize,
					ServerTime = DateTime.Now
				};
			}, TimeSpan.FromMinutes(30));

			return result;
		}
		#endregion
	}
}